﻿using MediatR;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using AutoMapper;
using Swashbuckle.AspNetCore.Swagger;
using Microsoft.EntityFrameworkCore;
using ShineTech.Infrastructure.Repositories;
using ShineTech.Domain.SeedWork;
using ShineTech.Web.Helpers;
using ShineTech.Domain.Models.Kanbans;
using ShineTech.Infrastructure.Repositories.Users;
using ShineTech.Infrastructure.Repositories.Missons;
using ShineTech.Domain.Models.Users;
using ShineTech.Infrastructure.Repositories.Kanbans;
using ShineTech.Infrastructure.Repositories.Shares;

namespace ShineTech.Web
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //services.AddDbContext<RentalContext>(opt => opt.UseInMemoryDatabase("rentaldb"));

            services.AddDbContext<RentalContext>(opt => 
            opt.UseSqlServer(@"Server=(localdb)\ProjectsV13;Initial Catalog=shinetest;Integrated Security=true"));
            //Server=(localdb)\ProjectsV12;Initial Catalog=mrestaurant;Integrated Security=true
            services.AddMediatR(typeof(Startup));

            services.AddScoped<IUserRepository, UserRepository>();
            services.AddScoped<IMissonRepository, MissonRepository>();
            services.AddScoped<IShareRepository, ShareRepository>();
            services.AddScoped<IKanbanRepository, KanbanRepository>();
            services.AddScoped<IUnitOfWork, UnitOfWork>();

            services.AddAutoMapper(typeof(Mapping));

            services.AddMvc().SetCompatibilityVersion(Microsoft.AspNetCore.Mvc.CompatibilityVersion.Version_2_2);


            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseCors(builder =>
            {
                builder.WithOrigins("*").AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().AllowCredentials();
            });
            app.UseSwagger();
            // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), specifying the Swagger JSON endpoint.
            app.UseSwaggerUI(c =>
            {
                c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1");
                c.RoutePrefix = string.Empty;
            });
            app.UseMvc();
        }
    }
}
